/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.gui;

import it.polimi.meteocal.sessionbeans.UserManager;
import it.polimi.meteocal.entities.User;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author miglie
 */
@Named
@RequestScoped
public class RegistrationBean implements Serializable {

    @EJB
    private UserManager um;

    private User user;

    public RegistrationBean() {
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String register() {
        /* System.out.println(user.getUsername());
        System.out.println(user.getEmail());
        System.out.println(user.getPassword()); */
        int result = um.save(user);
        if(result == -1){
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                               "Registraton failed: Username or Email already existing.", "Registration Failed"));          
            return null;
        }
        else return "login?faces-redirect=true";
    }

}
